<template>
  <view class="container">
    <uni-forms ref="form" :modelValue="form" labelWidth="120px" labelPosition="top">
      <view class="hidden-title">措施确认</view>
      <view class="background-white bor-top">
        <template v-for="(item, index) in form.deviceExamineCureEntityList">
          <uni-forms-item
            label="检修步骤"
            required
            :rules="[{ required: true, errorMessage: '检修步骤必填' }]"
            :key="item.id"
            :name="['deviceExamineCureEntityList', index, 'examineStep']"
          >
            <view class="form-item">
              <uni-easyinput
                v-model="form.deviceExamineCureEntityList[index].examineStep"
                placeholder="请输入检修步骤"
              />
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="安全措施"
            required
            :rules="[{ required: true, errorMessage: '安全措施必填' }]"
            :key="item.id"
            :name="['deviceExamineCureEntityList', index, 'measures']"
          >
            <view class="form-item">
              <uni-easyinput v-model="form.deviceExamineCureEntityList[index].measures" placeholder="请输入安全措施" />
            </view>
          </uni-forms-item>
          <uni-forms-item :name="['deviceExamineCureEntityList', index, 'stepStatus']" class="acceptance" required>
            <view slot="label">
              <view class="flex align-center">
                <view class="mr-20">措施是否落实</view>
                <view class="acceptance-list">
                  <view
                    class="acceptance-item"
                    v-for="(item, idx) in cureOptions"
                    :key="item.code"
                    :class="{ active: form.deviceExamineCureEntityList[index].stepStatus == item.dictValue }"
                    @click="form.deviceExamineCureEntityList[index].stepStatus = item.dictValue"
                  >
                    <image
                      class="icon-style"
                      v-show="form.deviceExamineCureEntityList[index].stepStatus != item.dictValue"
                      src="/static/images/work/dbd//selected_no.png"
                      mode="aspectFill"
                    ></image>
                    <image
                      class="icon-style"
                      v-show="form.deviceExamineCureEntityList[index].stepStatus == item.dictValue"
                      src="/static/images/work/dbd//selected_yes.png"
                      mode="aspectFill"
                    ></image>
                    <text class="content">{{ item.dictLabel }}</text>
                  </view>
                </view>
              </view>
            </view>
          </uni-forms-item>
          <uni-forms-item
            label="落实情况"
            required
            :rules="[{ required: true, errorMessage: '安全措施必填' }]"
            :key="item.id"
            :name="['deviceExamineCureEntityList', index, 'stepInfo']"
          >
            <view class="form-item">
              <uni-easyinput
                type="textarea"
                v-model="form.deviceExamineCureEntityList[index].stepInfo"
                placeholder="请输入落实情况"
                maxlength="50"
              ></uni-easyinput>
            </view>
          </uni-forms-item>
        </template>
      </view>
      <view class="background-white">
        <uni-forms-item name="opinionType" label="审查意见" class="acceptance" required>
          <uni-data-checkbox v-model="form.opinionType" :localdata="opinions" @change="radioChange"></uni-data-checkbox>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top" v-if="form.opinionType === 'REPULSE'">
        <uni-forms-item name="targetNodeId" label="选择回退节点">
          <cu-cell
            title="回退节点"
            required
            link
            placeholder="请选择"
            :description="taskNode"
            @click="rollbackClick()"
          ></cu-cell>
        </uni-forms-item>
      </view>
      <view class="background-white bor-top">
        <uni-forms-item name="opinion" label="审查意见说明">
          <uni-easyinput
            type="textarea"
            v-model="form.opinion"
            placeholder="简要说明审批意见"
            maxlength="50"
          ></uni-easyinput>
        </uni-forms-item>
      </view>
    </uni-forms>
    <CuSelect
      v-if="form.opinionType === 'REPULSE'"
      ref="rollbackDict"
      title="选择回退节点"
      :options="backNodes"
      @update:value="rollbackConfirm"
    ></CuSelect>
    <view class="submit-group">
      <button class="u-button" :ripple="true" @click="submit">提交</button>
    </view>
  </view>
</template>

<script>
import CuCellGroup from '@/components/cu-cell/cu-cell-group.vue'
import UserSelector from '@/components/user-selector/index.vue'
import CuSelect from '@/components/CuSelect/index.vue'
import { cure, getNode, queryInfo, getStep } from '@/api/repair/repair.js'

export default {
  components: {
    CuCellGroup,
    UserSelector,
    CuSelect
  },
  data() {
    return {
      cureOptions: [
        { dictLabel: '是', dictValue: 1 },
        { dictLabel: '否', dictValue: 2 }
      ],
      opinions: [
        { text: '合格', value: 'PASS' },
        { text: '不合格', value: 'REPULSE' }
      ],
      backNodes: [],
      id: '',
      procTaskId: '',
      form: {
        deviceExamineCureEntityList: [],
        opinionType: '',
        opinion: '',
        targetNodeId: ''
      },
      taskNode: '',
      targetNodeTypeValue: '',
      rules: {
        opinionType: {
          rules: [
            {
              required: true,
              errorMessage: '验收意见不能为空'
            }
          ]
        },
        targetNodeId: {
          rules: [
            {
              required: true,
              errorMessage: '回退节点不能为空'
            }
          ]
        },
        opinion: {
          rules: [
            {
              required: true,
              errorMessage: '审查意见说明不能为空'
            }
          ]
        }
      }
    }
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  onLoad(option) {
    if (!option.data) return
    let data = JSON.parse(option.data)
    this.id = data.id
    this.procTaskId = data.procTaskId
    this.methods(this.id)
  },
  methods: {
    methods(examineId) {
      getStep(examineId).then((res) => {
        res.data.map((m) => {
          this.form.deviceExamineCureEntityList.push({
            applyStepId: m.id,
            examineStep: m.examineStep,
            measures: m.measures,
            stepStatus: ''
          })
        })
      })
    },
    radioChange({ detail }) {
      if (detail.value === 'REPULSE') this.getBackNode(this.procTaskId)
    },
    getBackNode(procTaskId) {
      getNode(procTaskId).then((res) => {
        this.backNodes = res.data
        this.backNodes.map((m) => {
          m.label = m.nodeName
          m.value = m.nodeId
        })
      })
    },
    rollbackClick() {
      this.$refs.rollbackDict.open()
    },
    rollbackConfirm(obj) {
      this.taskNode = obj.name
      this.form.targetNodeId = obj.value
      let match = this.backNodes.find((n) => {
        return n.value === obj.value
      })
      this.targetNodeTypeValue = match.nodeTypeValue
    },
    submit() {
      this.$refs.form.validate().then((res) => {
        let data = {
          id: this.id,
          procTaskId: this.procTaskId,
          targetNodeId: this.form.targetNodeId,
          targetNodeTypeValue: this.targetNodeTypeValue,
          deviceExamineWorkflowEntity: {
            examineId: this.id,
            opinionType: this.form.opinionType,
            opinion: this.form.opinion
          },
          deviceExamineCureEntityList: this.form.deviceExamineCureEntityList
        }
        cure(data).then((res) => {
          this.$modal.showToast('处理成功')
          uni.$emit('refresh')
          uni.navigateBack()
        })
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  margin-bottom: 200rpx;

  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .photo {
      display: flex;
      justify-content: space-between;

      image {
        width: 214rpx;
        height: 214rpx;
      }
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.blue {
  color: #88b4c4;
}

.icon-style {
  width: 52rpx;
  height: 42rpx;
  padding-right: 10rpx;
}
.input {
  display: inline-block;
  width: 100%;
  background-color: #f5f5f5;
  padding: 20rpx;
  font-size: 28rpx;
  border-radius: 10rpx;
  height: 60rpx;
}
.acceptance {
  margin-bottom: 10rpx;
}
.acceptance-list {
  display: flex;

  .acceptance-item {
    display: flex;
    align-items: center;
    margin-left: 20rpx;
    color: #333;

    .icon-style {
      width: 50rpx;
      height: 40rpx;
      margin-right: 10rpx;
    }

    .content {
      flex: 1;
    }
  }
}
</style>
